package com.st.service.impl;

import cn.hutool.extra.mail.MailUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.OrderAttractionsdView;
import com.st.entity.Result;
import com.st.entity.User;
import com.st.entity.UserInfo;
import com.st.interceptor.UserInfoInterceptor;
import com.st.mapper.OrderAttractionsdViewMapper;
import com.st.mapper.UserInfoMapper;
import com.st.mapper.UserMapper;
import com.st.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {


    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private OrderAttractionsdViewMapper orderAttractionsdViewMapper;



    @Override
    public UserInfo findAllById(Integer id) {
        User user = userMapper.selectById(id);
        Integer userInfoId = user.getUserInfoId();
        UserInfo userInfo = userInfoMapper.selectById(userInfoId);
        return userInfo;
    }

    @Override
    public Result tNameAddUp(UserInfo userInfo) {
        if (userInfo == null) {
            return new Result(false, "请求参数异常");
        }
        int i = userInfoMapper.updateById(userInfo);
        return new Result(i > 0 ? true : false, "修改成功");


    }

    //绑定邮箱
    @Override
    public Result regist(UserInfo userInfo) {
            double random = Math.random();
            int code = (int) (random * 10000);
            Integer key = userInfo.getUserInfoId();
            redisTemplate.boundHashOps(key).put("mail", code+"");//把验证码存入Redis
            redisTemplate.expire(key,120, TimeUnit.SECONDS);//设置生命周期

            //发送邮件
            MailUtil.send(userInfo.getUserEmail(), "驴爸爸邮箱绑定", "您的验证码是"+ code , true);
            return new Result(true, "邮件发送成功");
        }




    @Override
    public Result check(UserInfo userInfo, String code) {

        //从redis获取验证码
        String mail = (String) redisTemplate.boundHashOps(userInfo.getUserInfoId()).get("mail");
        //校验验证码
        if (code == null || !code.equals(mail) ) {
            return  new Result(false,"验证码校验失败");
        }else {

            int i = userInfoMapper.updateById(userInfo);
            return new Result(i > 0 ? true : false, "修改成功");
        }

    }



    @Override
    public Result checkNote(String phone,String note) {
        String o = (String) redisTemplate.boundValueOps(phone + "_msg").get();
        if( note.equals(o) ){
           return new Result(true,"验证成功");
        }else {
            return new Result(false,"验证失败");
        }
    }

    @Override
    public List<OrderAttractionsdView> findOrderAllById(int id) {
        QueryWrapper<OrderAttractionsdView> qw = new QueryWrapper<>();
        qw.eq("user_id",id);
        List<OrderAttractionsdView> orderAttractionsdViews = orderAttractionsdViewMapper.selectList(qw);
        return orderAttractionsdViews;
    }


}
